package com.caojing.haolemai.ui.category.categorydclickactivity;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseActivity;
import com.caojing.haolemai.bean.categoryclickbean.CategoryClickBean;
import com.caojing.haolemai.bean.categoryclickbean.ClickResultsBean;
import com.caojing.haolemai.bean.categoryclickbean.ResultsBean;
import com.caojing.haolemai.config.UrlConfig;
import com.caojing.haolemai.ui.adapter.CategoryDetailGvAdapter;
import com.caojing.haolemai.view.MyGridView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CategoryClickActivity extends BaseActivity implements View.OnClickListener ,CategoryClickContract.View{


    private ImageView ivClickFanhui;
    private TextView tvClickBrandName;
    private TextView tvClickShaixuan;
    private TextView tvCategoryClickXiaoliang;
    private TextView tvClickJiage;
    private ImageView ivClickJiage;
    private RelativeLayout rlCateoryClickJiage;
    private TextView categoryClickZuixin;
    private MyGridView categoryDetailGv;
    private PullToRefreshScrollView categoryDetailSv;

    /*初始化标志位*/
    private int index1 = 0;
    public int PAGE_ID=1;
    public int ORDER_BY=3;
    public String SESSION_ID="865827023259062";

    private List<ResultsBean> totalList;
    private CategoryDetailGvAdapter adapter;
    private CategoryClickContract.Presenter presenter;
    private String brandId;
    private String categoryId;
    private String name;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_category_click;
    }

    @Override
    protected void initView() {
        ivClickFanhui= (ImageView) findViewById(R.id.iv_click_fanhui);
        tvClickBrandName= (TextView) findViewById(R.id.tv_click_brand_name);
        tvClickShaixuan= (TextView) findViewById(R.id.tv_click_brand_name);
        tvCategoryClickXiaoliang= (TextView) findViewById(R.id.tv_category_click_xiaoliang);
        rlCateoryClickJiage= (RelativeLayout) findViewById(R.id.rl_cateory_click_jiage);
        tvClickJiage= (TextView) findViewById(R.id.tv_click_Price);
        ivClickJiage= (ImageView) findViewById(R.id.iv_click_jiage);
        categoryClickZuixin= (TextView) findViewById(R.id.category_click_zuixin);
        categoryDetailGv= (MyGridView) findViewById(R.id.category_detail_Gv);
        categoryDetailSv= (PullToRefreshScrollView) findViewById(R.id.category_detail_sv);

        categoryDetailSv.setMode(PullToRefreshBase.Mode.BOTH);

        presenter=new CategoryClickPresenter(this);
        //初始化数据

        tvCategoryClickXiaoliang.setTextColor(Color.rgb(215,0,87));

        ivClickFanhui.setOnClickListener(this);
        tvClickShaixuan.setOnClickListener(this);
        tvCategoryClickXiaoliang.setOnClickListener(this);
        rlCateoryClickJiage.setOnClickListener(this);
        categoryClickZuixin.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        brandId = intent.getStringExtra("brandId");
        categoryId = intent.getStringExtra("categoryId");
        String brandName = intent.getStringExtra("brandName");

        name = intent.getStringExtra("name");
        if (!TextUtils.isEmpty(name)){
            presenter.initCategoryClickData(initSParms());
            tvClickBrandName.setText(name);
        }else{
            presenter.initCategoryClickData(initParms());
            tvClickBrandName.setText(brandName);
        }
        //初始化数据


        totalList = new ArrayList<>();
        adapter = new CategoryDetailGvAdapter(totalList,this);
        categoryDetailGv.setAdapter(adapter);

        categoryDetailSv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                categoryDetailSv.onRefreshComplete();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                PAGE_ID++;
                if (!TextUtils.isEmpty(name)){
                    presenter.initCategoryClickData(initSParms());
                }else{
                    presenter.initCategoryClickData(initParms());
                }
            }
        });


    }

    private HashMap<String,String> initSParms() {
        HashMap<String,String> map = new HashMap<>();
        map.put(UrlConfig.Key.INSTOCK,UrlConfig.DefaultValue.INSTOCK);
        map.put(UrlConfig.Key.PAGE_ID,PAGE_ID+"");
        map.put(UrlConfig.Key.PAGE_SIZE,UrlConfig.DefaultValue.PAGE_SIZE);
        map.put(UrlConfig.Key.ORDER_BY,ORDER_BY+"");
        map.put(UrlConfig.Key.KEYWORDS,name);
        return map;
    }

    /**
     * @return
     */
    private HashMap<String, String> initParms() {
        HashMap<String,String> map = new HashMap<>();
        //因为得到的两个值,有可能brandId没有，所以做了一下判断
        if (brandId!=null&&!"".equals(brandId)){
            map.put(UrlConfig.Key.INSTOCK,UrlConfig.DefaultValue.INSTOCK);
            map.put(UrlConfig.Key.PAGE_ID,PAGE_ID+"");
            map.put(UrlConfig.Key.PAGE_SIZE,UrlConfig.DefaultValue.PAGE_SIZE);
            map.put(UrlConfig.Key.ORDER_BY,ORDER_BY+"");
            map.put(UrlConfig.Key.BRAND_ID,brandId);
            map.put(UrlConfig.Key.CATEGORY_ID,categoryId);
            map.put(UrlConfig.Key.ACCESS_TOKEN_LEVEL1,UrlConfig.DefaultValue.ACCESS_TOKEN_LEVEL1);
            map.put(UrlConfig.Key.ACCESS_TOKEN_LEVEL2,UrlConfig.DefaultValue.ACCESS_TOKEN_LEVEL2);
            map.put(UrlConfig.Key.APP_ID,UrlConfig.DefaultValue.APP_ID);
            map.put(UrlConfig.Key.OS,UrlConfig.DefaultValue.OS);
            map.put(UrlConfig.Key.SESSION_ID,SESSION_ID);
            map.put(UrlConfig.Key.SITE_MASK,UrlConfig.DefaultValue.SITE_MASK);
            map.put(UrlConfig.Key.USER_ID,UrlConfig.DefaultValue.USER_ID);
            map.put(UrlConfig.Key.VERSION,UrlConfig.DefaultValue.VERSION);

            return map;
        }else {
            map.put(UrlConfig.Key.INSTOCK,UrlConfig.DefaultValue.INSTOCK);
            map.put(UrlConfig.Key.PAGE_ID,PAGE_ID+"");
            map.put(UrlConfig.Key.PAGE_SIZE,UrlConfig.DefaultValue.PAGE_SIZE);
            map.put(UrlConfig.Key.ORDER_BY,ORDER_BY+"");
            map.put(UrlConfig.Key.CATEGORY_ID,categoryId);
            map.put(UrlConfig.Key.ACCESS_TOKEN_LEVEL1,UrlConfig.DefaultValue.ACCESS_TOKEN_LEVEL1);
            map.put(UrlConfig.Key.ACCESS_TOKEN_LEVEL2,UrlConfig.DefaultValue.ACCESS_TOKEN_LEVEL2);
            map.put(UrlConfig.Key.APP_ID,UrlConfig.DefaultValue.APP_ID);
            map.put(UrlConfig.Key.OS,UrlConfig.DefaultValue.OS);
            map.put(UrlConfig.Key.SESSION_ID,SESSION_ID);
            map.put(UrlConfig.Key.SITE_MASK,UrlConfig.DefaultValue.SITE_MASK);
            map.put(UrlConfig.Key.USER_ID,UrlConfig.DefaultValue.USER_ID);
            map.put(UrlConfig.Key.VERSION,UrlConfig.DefaultValue.VERSION);

            return map;
        }


    }

    @Override
    public void onClick(View view) {
        PAGE_ID=1;
        //清空数据源，才能加载数据，价格和更新才能完成
        totalList.clear();
        switch (view.getId()) {
            case R.id.iv_click_fanhui:
                finish();
                break;
            case R.id.tv_click_shaixuan:
                break;
            case R.id.tv_category_click_xiaoliang:
                index1=0;
                tvClickJiage.setTextColor(Color.BLACK);
                ivClickJiage.setImageResource(R.mipmap.up_gray);
                categoryClickZuixin.setTextColor(Color.BLACK);
                tvCategoryClickXiaoliang.setTextColor(Color.rgb(215,0,87));
                ORDER_BY=3;
                break;
            case R.id.rl_cateory_click_jiage:
                tvClickJiage.setTextColor(Color.rgb(215,0,87));
                categoryClickZuixin.setTextColor(Color.BLACK);
                tvCategoryClickXiaoliang.setTextColor(Color.BLACK);
                if (index1%2==0){
                    ivClickJiage.setImageResource(R.mipmap.up_red);
                    ORDER_BY=2;
                }else{
                    ivClickJiage.setImageResource(R.mipmap.up_red_down);
                    ORDER_BY=4;
                }
                index1++;
                break;
            case R.id.category_click_zuixin:
                index1=0;
                tvClickJiage.setTextColor(Color.BLACK);
                ivClickJiage.setImageResource(R.mipmap.up_gray);
                categoryClickZuixin.setTextColor(Color.rgb(215,0,87));
                tvCategoryClickXiaoliang.setTextColor(Color.BLACK);
                ORDER_BY=1;
                break;
        }

        if (!TextUtils.isEmpty(name)){
            presenter.initCategoryClickData(initSParms());
        }else{
            presenter.initCategoryClickData(initParms());
        }
    }

    @Override
    public void onSuccessCategoryClick(CategoryClickBean brandBean) {
        ClickResultsBean results = brandBean.getResults();
        List<ResultsBean> resultsResults = results.getResults();
        totalList.addAll(resultsResults);
        Log.i("TAG"," totalList========="+ totalList.toString());
        adapter.notifyDataSetChanged();
        categoryDetailSv.onRefreshComplete();
    }

    @Override
    public void onFailCategoryClick(String msg) {
        Toast.makeText(CategoryClickActivity.this,msg,Toast.LENGTH_SHORT).show();
    }
}
